package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.pojo.Direction;
import com.tpyule.tpclass.service.DirectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @date 2020/2/26 0026 下午 3:14
 */
@RestController
@RequestMapping("direction")
public class DirectionController extends BaseController {
    @Autowired
    DirectionService directionService;

    @RequestMapping("/add")
    @ResponseBody
    public void addDirection(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        ResultBean resultBean=null;
        if (name!=null){
            Direction direction = new Direction();
            direction.setName(name);
            int result = directionService.addDirection(direction);
            if(result==1){
                resultBean = new SuccessResultBean();
            }else{
                resultBean = new FailResultBean("添加失败，请重试");
            }
        }else {
            resultBean = new FailResultBean("数据错误");
        }
        this.setAjaxResponseResult(response, resultBean);
    }

    @RequestMapping("getAllDirection")
    @ResponseBody
    public void getAllDirection(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        List<Direction> directions=directionService.getAllDirection();
        if(directions!=null){
            resultBean=new SuccessResultBean<List<Direction>>(directions,"获取成功");
        }else{
            resultBean=new FailResultBean<>("获取失败");
        }
        try {
            this.setAjaxResponseResult(response,resultBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("getDirectionBySort")
    @ResponseBody
    public void getDirectionBySortId(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        int sortId=Integer.parseInt(request.getParameter("sortId"));
        Direction direction=directionService.getSortByDirectionId(sortId);
        if(direction!=null){
            resultBean=new SuccessResultBean<Direction>(direction,"获取成功");
        }else{
            resultBean=new FailResultBean<>("获取失败");
        }
        try {
            this.setAjaxResponseResult(response,resultBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("getSizeDirection")
    @ResponseBody
    public void getSizeDirection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        try {
            int result = directionService.getSizeDirection();
            resultBean=new SuccessResultBean(result,"获取成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("getListDirection")
    @ResponseBody
    public void getListDirection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        try {
            String queryStr = request.getParameter("commonQueryData");
            if(queryStr!=null){
                CommonQueryDto commonQueryDto = objectMapper.readValue(queryStr,CommonQueryDto.class);
                if (commonQueryDto!=null){
                    List<Direction> directions = directionService.getListDirection(commonQueryDto);
                    if(directions!=null){
                        resultBean = new SuccessResultBean(directions, "成功");
                    }else {
                        resultBean = new FailResultBean("数据错误");
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("update")
    @ResponseBody
    public void updateDirection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        Direction direction = null;
        try {
            String directionStr=request.getParameter("directionData");
            if(directionStr!=null){
                direction = objectMapper.readValue(directionStr, Direction.class);
                int result = directionService.updateDirection(direction);
                if (result>0){
                    resultBean = new SuccessResultBean("成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("delete")
    @ResponseBody
    public void deleteDirection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        Direction direction = null;
        try {
            String directionStr=request.getParameter("directionData");
            if(directionStr!=null){
                direction = objectMapper.readValue(directionStr, Direction.class);
                resultBean = directionService.deleteDirection(direction);
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
